/**************************************************************************
 * Copyright?2004-2016 浙江盘石信息技术股份有限公司
 * All rights reserved.
 *
 * 项目名称：互金网络平台
 * 版权说明：本软件属浙江盘石信息技术股份有限公司所有，在未获浙江盘石信息技术股份有限公司正式授权情况下，
 *          任何企业和个人，不能获取、阅读、安装、传播本软件涉及的任何受知识产权保护的内容。   
 ***************************************************************************/
package com.alnex.activiti;

import java.io.InputStream;
import java.util.List;
import java.util.zip.ZipInputStream;

import org.activiti.engine.*;
import org.activiti.engine.repository.*;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Test;

/**
 * 测试类
 * 作用：测试activiti所需要的25张表的生成
 */

public class demo {

    @Test
    public void t0() {
        //条件：1.activiti配置文件名称：activiti.cfg.xml   2.bean的id="processEngineConfiguration"
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        System.out.println(processEngine);

        // HistoryService historyService = processEngine.getHistoryService();
    }


    @Test
    public void t1() {
        ProcessEngineConfiguration processEngineConfiguration = ProcessEngineConfiguration.
                createProcessEngineConfigurationFromResource("activiti.cfg.xml", "processEngineConfiguration");

        ProcessEngine processEngine = processEngineConfiguration.buildProcessEngine();

        // System.out.println(processEngine);

        RepositoryService repositoryService = processEngine.getRepositoryService();
    }

    //部署流程
    @Test
    public void t3() {
        RepositoryService repositoryService = ProcessEngines.getDefaultProcessEngine().getRepositoryService();

        //流对象
        InputStream ras1 = this.getClass().getClassLoader().getResourceAsStream("diagram/test.bpmn");
        InputStream ras2 = this.getClass().getClassLoader().getResourceAsStream("diagram/test.png");

        //部署对象
        Deployment activiti = repositoryService.createDeployment()
                // addClasspathResource("diagram/test.bpmn")//bpmn文件
                // .addClasspathResource("diagram/test.png")//图片文件
                .addInputStream("test.bpmn", ras1)
                .addInputStream("test.png", ras2)
                .name("工作流程")
                .deploy();

        System.out.println(activiti.getId());
        System.out.println(activiti.getName());
    }

    /**
     * 压缩包部署流程
     * <p>
     * act_re_procdef
     * act_re_deployment 部署信息
     * act_ge_property 流程定义的一些信息
     * act_ge_bytearray 流程定义的bpmn文件及png文件
     */
    @Test
    public void t3_0() {
        RepositoryService repositoryService = ProcessEngines.getDefaultProcessEngine().getRepositoryService();

        //流对象
        InputStream ras1 = this.getClass().getClassLoader().getResourceAsStream("diagram/ho.zip");

        ZipInputStream zipInputStream = new ZipInputStream(ras1);

        //部署对象
        Deployment activiti = repositoryService.createDeployment().addZipInputStream(zipInputStream).name("工作流程").deploy();

        System.out.println(activiti.getId());
        System.out.println(activiti.getName());
    }

    //启动流程
    @Test
    public void t3_1_1() {
        //启动流程服务
        RuntimeService runtimeService = ProcessEngines.getDefaultProcessEngine().getRuntimeService();
        ProcessInstance processInstance = runtimeService.startProcessInstanceById("myProcess_1:2:2504");


        //4.输出实例的相关信息
        System.out.println("流程部署ID" + processInstance.getDeploymentId());//null
        System.out.println("流程定义ID" + processInstance.getProcessDefinitionId());//holiday:1:4
        System.out.println("流程实例ID" + processInstance.getId());//2501
        System.out.println("活动ID" + processInstance.getActivityId());//null
    }

    @Test
    public void t4() {
        TaskService taskService = ProcessEngines.getDefaultProcessEngine().getTaskService();

        List<Task> list = taskService.createTaskQuery().processDefinitionId("myProcess_1:2:2504").taskAssignee("san").list();

        for (Task task : list) {
            //4.任务列表的展示
            System.out.println("流程实例ID:" + task.getProcessInstanceId());
            System.out.println("任务ID:" + task.getId());
            System.out.println("任务负责人:" + task.getAssignee());
            System.out.println("任务名称:" + task.getName());
        }

    }

    //流程信息
    @Test
    public void t3_1() {

        RepositoryService repositoryService = ProcessEngines.getDefaultProcessEngine().getRepositoryService();
        //查询器
        DeploymentQuery deploymentQuery = repositoryService.createDeploymentQuery();
        ProcessDefinitionQuery processDefinitionQuery = repositoryService.createProcessDefinitionQuery();

        List<ProcessDefinition> test = processDefinitionQuery.processDefinitionKey("myProcess_1").orderByProcessDefinitionVersion().desc().list();

        System.out.println(test.size());

        for (ProcessDefinition processDefinition : test) {
            System.out.println("流程定义ID：" + processDefinition.getId());
            System.out.println("流程定义名称：" + processDefinition.getName());
            System.out.println("流程定义的Key：" + processDefinition.getKey());
            System.out.println("流程定义的版本号：" + processDefinition.getVersion());
            System.out.println("流程部署的ID:" + processDefinition.getDeploymentId());
        }
        /*
        流程定义ID：myProcess_1:2:2504
        流程定义名称：每日工作
        流程定义的Key：myProcess_1
        流程定义的版本号：2
        流程部署的ID:2501
        流程定义ID：myProcess_1:1:4
        流程定义名称：每日工作
        流程定义的Key：myProcess_1
        流程定义的版本号：1
        流程部署的ID:1
         */
    }

    //任务查询
    @Test
    public void t5() {
        String assignee = "yi";

        TaskService taskService = ProcessEngines.getDefaultProcessEngine().getTaskService();

        List<Task> list = taskService.createTaskQuery().taskAssignee(assignee).list();

        for (Task task : list) {
            System.out.println("流程实例ID：" + task.getProcessInstanceId());
            System.out.println("任务id：" + task.getId());
            System.out.println("任务负责人：" + task.getAssignee());
            System.out.println("任务名称：" + task.getName());
        }
    }


    //任务查询
    @Test
    public void t6() {
        TaskService taskService = ProcessEngines.getDefaultProcessEngine().getTaskService();

        // List<Task> list = taskService.createTaskQuery().processDefinitionKey("myProcess_1").taskAssignee(assignee).list();
        List<Task> list = taskService.createTaskQuery().processDefinitionKey("holiday").list();
        for (Task task : list) {
            System.out.println("流程实例ID：" + task.getProcessInstanceId());
            System.out.println("任务id：" + task.getId());
            System.out.println("任务负责人：" + task.getAssignee());
            System.out.println("任务名称：" + task.getName());
        }
    }

    //任务处理
    @Test
    public void t7() {
        String taskId = "";

        TaskService taskService = ProcessEngines.getDefaultProcessEngine().getTaskService();

        taskService.complete(taskId);
    }


    //挂起 激活流程定义
    @Test
    public void t8() {
        //流程定义的id
        String processDefinitionId = "holiday:1:4";
        RepositoryService repositoryService = ProcessEngines.getDefaultProcessEngine().getRepositoryService();

        //流程定义
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().processDefinitionId(processDefinitionId).singleResult();
        //是否暂停
        boolean suspended = processDefinition.isSuspended();

        System.out.println("执行结果：" + suspended);

        if (suspended) {
            // 如果暂停则激活，这里将流程定义下的所有流程实例全部激活
            repositoryService.activateProcessDefinitionById(processDefinitionId, true, null);
            System.out.println("流程定义：" + processDefinitionId + "激活");
        } else {
            //如果激活则挂起，这里将流程定义下的所有流程实例全部挂起
            repositoryService.suspendProcessDefinitionById(processDefinitionId, true, null);
            System.out.println("流程定义：" + processDefinitionId + "挂起");
        }
    }

    //单个流程实例挂起
    @Test
    public void t9(){
        //流程实例的id
        String processInstanceId = "12501";
        RuntimeService runtimeService = ProcessEngines.getDefaultProcessEngine().getRuntimeService();

        ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();

        boolean suspended = processInstance.isSuspended();

        if (suspended) {
            // 如果暂停则激活，这里将流程定义下的所有流程实例全部激活
            runtimeService.activateProcessInstanceById(processInstanceId);
            System.out.println("流程定义：" + processInstanceId + "激活");
        } else {
            //如果激活则挂起，这里将流程定义下的所有流程实例全部挂起
            runtimeService.suspendProcessInstanceById(processInstanceId);
            System.out.println("流程定义：" + processInstanceId + "挂起");
        }
    }

    @Test
    public void t10(){
        // Activiti使用 UEL 表达式，UEL 是 java EE6规范的一部分，UEL（Unified Expression Language）即
        // 统一表达式语言，activiti支持两个 UEL表达式：UEL-value 和 UEL-method。
    }
}
